<template>
  <div class="app-container">
    <el-tabs v-model="activeName">
      <el-tab-pane label="用户人均时长统计" name="user">
        <div class="filter-container">
            <el-select
                v-model="listQuery.date_time"
                style="width: 150px"
                class="filter-item"
                @change="handleFilter"
              >
                <el-option v-for="(item, index) in last7Days" :key="item" :label="item" :value="item" />
              </el-select>

            <el-button
            plain size="small"
              v-waves
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter"
            >搜索</el-button>

            <el-button
            plain size="small"
              v-waves
              class="filter-item btn"
              type="primary"
              icon="el-icon-refresh"
              @click="handleCancel"
            >重置</el-button>
            </div>

            <el-table
            :key="tableKey"
            v-loading="listLoading"
            :data="list"
            border
            fit
            highlight-current-row
            style="width: 100%;"
            >

            <el-table-column label="包" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ pkgs[row.pkg] }}</span> 
              </template>
            </el-table-column>
            <el-table-column label="厂商" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ row.brand }}</span> 
              </template>
            </el-table-column>
            <el-table-column label="用户使用总时间" prop="sum_time" align="center">
              <template slot-scope="{row}">
                <span>{{ row.sum_time}} 秒</span> 
              </template>
            </el-table-column>
            <el-table-column label="pv数量" prop="pv" align="center"> </el-table-column>
            <el-table-column label="人均时长" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ (row.sum_time / row.pv).toFixed(2) }} 秒</span> 
              </template>
            </el-table-column>
            <el-table-column label="时间" min-width="100" align="center">
              <template slot-scope="{row}">
                <span>{{ row.create_time }}</span>
              </template>
            </el-table-column>
            </el-table>

      </el-tab-pane>


      <el-tab-pane label="用户人均时长详细" name="user_info">
        <div class="filter-container">

            <el-date-picker
              v-model="listQuery2.date"
              type="date"
              value-format="yyyy-MM-dd"
              :picker-options="pickerOptions2"
              placeholder="选择日期">
            </el-date-picker>
            
            <el-select
              v-model="listQuery2.pkg"
              style="width: 150px"
              class="filter-item"
            >
              <el-option label="全部包" :value="''" />
              <el-option v-for="(v, k) in pkgs" :key="k" :label="v" :value="k" />
              
            </el-select>
            <el-select
                v-model="listQuery2.brand"
                clearable
                filterable
                style="width: 150px"
                class="filter-item"
                placeholder="厂商"
              >
              <el-option key="100" label="所有厂商" value=""></el-option>
              <el-option
                v-for="(v, k, index) in brand1"
                :key="index"
                :label="v"
                :value="k"
              />
            </el-select>

            <el-input
              v-model="listQuery2.oaid"
              placeholder="搜索设备id"
              style="width: 220px;"
              class="filter-item"
            />

        
            <el-button
            plain size="small"
              v-waves
              class="filter-item"
              type="primary"
              icon="el-icon-search"
              @click="handleFilter2"
            >搜索</el-button>

            <el-button
            plain size="small"
              v-waves
              class="filter-item btn"
              type="primary"
              icon="el-icon-refresh"
              @click="handleCancel2"
            >重置</el-button>
          </div>

          <el-table
            v-loading="listLoading"
            :data="user_info_list"
            border
            fit
            highlight-current-row
            style="width: 100%;"
          >
            <el-table-column label="设备id" porp="oaid" min-width="50" align="center">
              <template slot-scope="{row}">
                <span>{{ row.oaid }}</span>
              </template>
            </el-table-column>
            <el-table-column label="包" min-width="50" align="center">
              <template slot-scope="{row}">
                <span>{{pkgs[row.pkg] }}</span>
              </template>
            </el-table-column>
            <el-table-column label="厂商" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ row.brand }}</span> 
              </template>
            </el-table-column>

            <el-table-column label="进入时间" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ row.start_time }}</span> 
              </template>
            </el-table-column>

            <el-table-column label="离开时间" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ row.end_time }}</span> 
              </template>
            </el-table-column>

            <el-table-column label="在应用内时间(秒)" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ row.second }}</span> 
              </template>
            </el-table-column>

            <el-table-column label="创建时间" min-width="" align="center">
              <template slot-scope="{row}">
                <span>{{ row.create_time }}</span>
              </template>
            </el-table-column>

          </el-table>
      
          <pagination
            v-show="user_info_total > 0"
            :total="user_info_total"
            class="pageShow"
            :page.sync="listQuery2.page"
            :limit.sync="listQuery2.limit"
            @pagination="getUserTimeInfo"
          />
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import {
  getUsageTime,
  getUsageTimeInfo,
  getPkgList
} from "@/api/adv_admin/config";
import waves from "@/directive/waves"; // waves directive
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination
import {brand1} from '@/utils/index'
export default {
  name: "Control",
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      activeName: 'user',
      brand1,
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 20,
        date_time: "",
      },
      error_type: {},
      pkgs:{},
      last7Days:[],
      // 用户人均时长详细
      pickerOptions2:{
        disabledDate(time) {
          return time.getTime() > Date.now();
        },
      },
      listQuery2:{
        page: 1,
        limit: 20,
        pkg: "",
        brand: '',
        date: ''
      },
      user_info_list: [],
      user_info_total: 0
    };
  },
  created() {
    this.getPkgs();
    this.get7Day()
    this.getList();
  //   this.setDialogWidth();
  },
  mounted() {
    window.onresize = () => {
      return (() => {
        this.setDialogWidth();
      })();
    };
  },
  methods: {
    getPkgs(){
        getPkgList().then((res)=>{
          this.pkgs = res.DATA.list
        })
      },
    setDialogWidth() {
      const val = document.body.clientWidth;
      const def = 600; // 默认宽度
      if (val < def) {
        this.dialogWidth = "100%";
      } else {
        this.dialogWidth = def + "px";
      }
    },
    getList() {
      this.listLoading = true;
      getUsageTime(this.listQuery).then((response) => {
        this.list = response.DATA.list;
        this.total = response.DATA.total;
        this.listLoading = false;
      });
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    handleCancel(){
      this.listQuery = {
        page: 1,
        limit: 20,
        date_time: ""
      }
      this.getList()
    },
    get7Day(){
      // 获取当前日期
      const today = new Date();

      // 格式化为 YYYY-MM-DD
      const formatDate = (date) => {
          const year = date.getFullYear();
          const month = String(date.getMonth() + 1).padStart(2, '0');  // 月份从0开始
          const day = String(date.getDate()).padStart(2, '0');
          return `${year}-${month}-${day}`;
      };

      // 获取最近7天的日期
      const getLast7Days = () => {
          const dates = [];
          
          for (let i = 0; i < 7; i++) {
              // 创建一个新日期对象，避免直接修改原始 `today` 对象
              const date = new Date(today);
              date.setDate(today.getDate() - i);
              dates.push(formatDate(date)); // 格式化日期并存储
          }
          
          return dates;
      };

      // 获取最近7天的日期
      this.last7Days = getLast7Days();
      this.listQuery.date_time = this.last7Days[0]
      console.log(this.last7Days);  // 输出最近7天的日期，例如：["2024-11-11", "2024-11-10", "2024-11-09", "2024-11-08", "2024-11-07", "2024-11-06", "2024-11-05"]

    },
    // 用户人均时长详情
    
    getUserTimeInfo(){
      this.listLoading = true;
      getUsageTimeInfo(this.listQuery2).then((response) => {
        this.user_info_list = response.DATA.list;
        this.user_info_total = response.DATA.total;
        this.listLoading = false;
        console.log(this.user_info_list)
      });
    },
    handleFilter2(){
      this.listQuery.page = 1;
      this.getUserTimeInfo();
    },
    handleCancel2(){
        this.listQuery2 = {
          page: 1,
          limit: 20,
          pkg: "",
          brand: '',
          date: '',
          oaid: ''
        }
        this.getUserTimeInfo()
      },
  
  },
};
</script>

<style lang="scss" scoped>
.show-pwd {
  position: absolute;
  right: 10px;
  top: 0;
  font-size: 16px;
  color: #ccc;
  cursor: pointer;
  user-select: none;
}
.push {
  display: inline-block;
  width: 20px;
  height: 20px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  margin-left: 5px;
}
.pageHidden {
  display: none;
}
.btn {
  margin-left: 0;
}
.fullwidth {
  width: 450px;
  margin-left: 50px;
}
@media (max-width: 550px) {
  .pageShow {
    display: none;
  }
  .pageHidden {
    display: block;
  }
  .fullwidth {
    width: 100%;
    margin-left: 0;
  }
}
</style>
